﻿using System;
using System.Collections;
using System.Collections.Generic;
using UBlockly.UI;
using UnityEngine;

namespace UBlockly.Compotent
{
    public class UBlockAnchor : BlockAnchor,IUAnchor
    {
        float minHeight = 0;
        private float yContentOffset = 0;

        public void InitMinHeiht(float height)
        {
            minHeight = height;
        }
        void IUAnchor.Resize()
        {
            if (GetComponent<SlotTypeBlock>() != null)
                yContentOffset = BlockGraphicSize.VoidRContentVOffset;
            UpdateSubAuchorList();
            if (subAnchorList == null || subAnchorList.Count == 0)
                return;
            float width = 0;
            float height = 0;
            float lastXOffset = BlockGraphicSize.LeftBorderSpan;
            //计算长度
            foreach (var b in subAnchorList)
            {
                if (b as UDescAnchor || b as UPackerAnchor)
                {
                    b.Resize();
                }
                else
                {
                    throw new System.Exception("UBlockAnchor UnKnowerror");
                }
            }
            foreach (var b in subAnchorList)
            {
                if (b as UDescAnchor)
                {
                    b.SetPosition(new Vector2(BlockGraphicSize.LeftBorderSpan, yContentOffset));
                    lastXOffset = b.GetPostion().x + b.GetSize().x + BlockGraphicSize.BlockDescPackerSpace;
                    //height = b.GetSize().y > height ? b.GetSize().y : height;
                    width = b.GetPostion().x + b.GetSize().x + BlockGraphicSize.RightBorderSpan;
                }
                else if (b as UPackerAnchor)
                {
                    b.SetPosition(new Vector2(lastXOffset, yContentOffset));
                    lastXOffset = b.GetPostion().x + b.GetSize().x;
                    height = b.GetSize().y > height ? b.GetSize().y : height;
                    height += BlockGraphicSize.PackerBlockBorder * 2;
                    width = b.GetPostion().x + b.GetSize().x + BlockGraphicSize.RightBorderSpan;
                }
            }
            height += BlockGraphicSize.PackerBlockBorder * 2;
            height = height > minHeight ? height : minHeight;
            width = width > BlockGraphicSize.BlockMinLength ? width : BlockGraphicSize.BlockMinLength;
            SetSize(new Vector2(width, height));
        }

        void IUAnchor.SetPosition(Vector2 pos)
        {
            RectTransform.anchoredPosition = pos;
        }
        Vector2 IUAnchor.GetSize()
        {
            return base.GetSize();
        }
        Vector2 IUAnchor.GetPostion()
        {
            return RectTransform.anchoredPosition;
        }
    }
}

